﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using suirui.ZhuMu.Log;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace suirui.ZhuMu.Middlewares
{
    public class GlobalExceptionMildd
    {
        private readonly RequestDelegate next;
        public GlobalExceptionMildd(RequestDelegate next)
        {
            this.next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                await HandleExceptionAsync(context, ex);
            }
            //finally
            //{
            //    var statusCode = context.Response.StatusCode;
            //    if (statusCode != StatusCodes.Status200OK)
            //    {
            //        Enum.TryParse(typeof(HttpStatusCode), statusCode.ToString(), out object message);
            //        await ExceptionHandlerAsync(context, message.ToString());
            //    }
            //}
        }

        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            if (exception == null) return;
            await WriteExceptionAsync(context, exception).ConfigureAwait(false);
        }

        private async Task WriteExceptionAsync(HttpContext context, Exception exception)
        {
            //记录日志
            DefaultLogFactory.Instance.Exception_Loger.Error(exception.Message, exception);
            //返回友好的提示
            var response = context.Response;

            //状态码
            if (exception is UnauthorizedAccessException)
                response.StatusCode = (int)HttpStatusCode.Unauthorized;
            else if (exception is Exception)
                response.StatusCode = (int)HttpStatusCode.BadRequest;

            response.ContentType = context.Request.Headers["Accept"];

            response.ContentType = "application/json";
            await response.WriteAsync(JsonConvert.SerializeObject(new { state = 400, message = "出现未知异常" })).ConfigureAwait(false);
        }
    }

    public static class VisitLogMiddlewareExtensions
    {
        public static IApplicationBuilder UseGlobalException(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<GlobalExceptionMildd>();
        }
    }
}
